<template>
    <div class="wrapper">
        <header>
            <div class="title">
                <p>商家列表</p>
            </div>
        </header>


        <!----商家列表----->
        <ul class="business">
            <li v-for="item in businessArr" @click="toBusinessInfo(item.businessId)">
                <img :src=item.businessImg>
                <div class="business-img-quantity" v-show="item.quantity > 0">{{ item.quantity }}</div>
                <div class="text">
                    <h3>{{ item.businessName }}</h3>
                    <p>{{ item.starPrice }}起送 | {{ item.deliveryPrice }}配送</p>
                    <p>{{ item.businessExplain }}</p>
                </div>
            </li>
        </ul>

        <Footer> </Footer>
    </div>
</template>

<script>
import Footer from '../components/Footer.vue';
export default {
    name: 'BusinessList',
    data() {
        return {
            orderTypeId: this.$route.query.orderTypeId,
            businessArr: [],
            user:{}
        }
    },
    created() {
        this.user = this.$getSessionStorage('user');
        this.$axios.post('BusinessController/listBusinessByOrderTypeId', this.$qs.stringify({
            orderTypeId: this.orderTypeId
        })).then(response => {
            this.businessArr = response.data;
            if(this.user!=null){
                this.listCart();
            }
        }).catch(error => {
            console.error(error);
        });
    },
    components: {
        Footer
    },
    methods: {
        listCart() {
            this.$axios.post('CartController/listCart', this.$qs.stringify({
                userId: this.user.userId
            })).then(response => {
                let cartArr = response.data;
                //遍历所有食品列表
                for (let businessItem of this.businessArr) {
                    businessItem.quantity = 0;
                    for (let cartItem of cartArr) {
                        if (cartItem.businessId == businessItem.businessId) {
                            businessItem.quantity += cartItem.quantity;
                        }
                    }
                }
                this.businessArr.sort();
            }).catch(error => {
                console.error(error);
            });
        },
        toBusinessInfo(businessId) {
            this.$router.push({ path: '/businessInfo', query: { businessId: businessId } });
        }
    }
}
</script>

<style scoped>
/***********头部*********/
.wrapper {
    width: 100%;
    height: 100%;
}

.wrapper header {
    width: 100%;
    height: 12vw;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #51b9fe;
}

.wrapper header .title p {
    font-size: 4.5vw;
    color: #fff;
}


/***********底部不动的菜单************/
.wrapper .footer {
    justify-content: space-around;
    display: flex;
    align-items: center;
    width: 100%;
    height: 14vw;
    border-top: solid 1px #ddd;
    background-color: #fff;

    position: fixed;
    left: 0;
    bottom: 0;
}

.wrapper .footer li {
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    user-select: none;
    cursor: pointer;
    color: #999;
}

.wrapper .footer li img {
    width: 5vw;
    height: 5w;
}

.wrapper .footer li p {
    font-size: 3vw;
}

.wrapper .footer li i {
    font-size: 5vw;
}


/***********商家列表************/
.wrapper .business {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
}

.wrapper .business li {
    width: 100vw;
    height: 20vw;
    display: flex;
    user-select: none;
    align-items: center;
    margin-top: 1vw;
    border-bottom: solid 1px #e7dddd;
}

.wrapper .business li img {
    width: 18vw;
    height: 18vw;
    margin-left: 2vw;
}

.wrapper .business li .business-img-quantity {
    width: 5vw;
    height: 5vw;
    background-color: red;
    color: #fff;
    font-size: 3.6vw;
    border-radius: 2.5vw;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    left: 17vw;
    margin-top: -17vw;                    /*absolute注意，但是手机端看不出问题*/
 }

.wrapper .business li .text {
    width: 100vw;
    display: flex;
    flex-direction: column;
    margin-left: 3vw;
}

.wrapper .business li .text h3 {
    font-size: 4vw;
    color: #333;
}

.wrapper .business li .text p {
    font-size: 3vw;
    color: #999;
    margin-top: 1vw;
}
</style>